/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.rentalofconstructionmachinery.service.impl;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dao.RentalDAO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.CustomerDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.MachineDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.RentalDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.converters.CustomerDTOConverter;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.converters.MachineDTOConverter;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.converters.RentalDTOConverter;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Rental;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.RentalService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author David Domonkos
 */
@Service("rentalService")
public class RentalServiceImpl implements RentalService {
    
    @Autowired
    private RentalDAO rentalDAO;
    
    public void setRentalDAO(RentalDAO rentalDAO) {
        this.rentalDAO = rentalDAO;
    }

    @Override
    @Transactional
    public void addRental(RentalDTO rental) {
        if (rental == null) {
            throw new IllegalArgumentException("'rental' must not be null!");
        }
        rentalDAO.createRental(RentalDTOConverter.dtoToEntity(rental));
    }

    @Override
    @Transactional
    public void removeRental(RentalDTO rental) {
        if (rental == null) {
            throw new IllegalArgumentException("'rental' must not be null!");
        }
        rentalDAO.deleteRental(RentalDTOConverter.dtoToEntity(rental));
    }

    @Override
    @Transactional
    public void modifyRental(RentalDTO rental) {
        if (rental == null) {
            throw new IllegalArgumentException("'rental' must not be null!");
        }
        rentalDAO.updateRental(RentalDTOConverter.dtoToEntity(rental));
    }

    @Override
    @Transactional
    public List<RentalDTO> getAllRentals() {
        return RentalDTOConverter.entityToDTOList(rentalDAO.getAllRentals());
    }

    @Override
    @Transactional
    public RentalDTO getRentalById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("'id' must not be null!");
        }
        return RentalDTOConverter.entityToDTO(rentalDAO.getRentalById(id));
    }

    @Override
    @Transactional
    public List<RentalDTO> getRentalsByCustomer(CustomerDTO customer) {
        if (customer == null) {
            throw new IllegalArgumentException("'customer' must not be null!");
        }
        List<Rental> entities = rentalDAO.getRentalsByCustomer(CustomerDTOConverter.dtoToEntity(customer));
        return RentalDTOConverter.entityToDTOList(entities);
    }

    @Override
    @Transactional
    public List<RentalDTO> getRentalsByMachine(MachineDTO machine) {
        if (machine == null) {
            throw new IllegalArgumentException("'machine' must not be null!");
        }
        List<Rental> entities = rentalDAO.getRentalsByMachine(MachineDTOConverter.dtoToEntity(machine));
        return RentalDTOConverter.entityToDTOList(entities);
    }
    
}
